import { observable, action ,runInAction,toJS} from "mobx";



export default class Configura{
  
  @observable loading={};
  // where查询条件
  // @observable where={};

  // 配置信息 系统  APP
  @observable setting={sys:{},app:{},postage:[]};
  // 显示快递公司输入框
  @observable companyVisible=false;
  // 快递公司信息
  @observable postage={};
  // 右侧列表高度
  @observable contHeight=0;

  // ----------------专题相关
  // 分类
  @observable treeData=[];
  // 选择上级分类
  @observable options=[];
  // 树形所选节点
  @observable selectedKeys=[];
  // // 表格分页数据
  // @observable paging={};
  // // 表单数据
  // @observable form={insert:{},update:{}};
  // // 专题选择影视搜索关键字
  // @observable searchKey="";
  // // 专题选择影视搜索结果列表
  // @observable searchData=[];

  constructor(root){
    this.root = root;
  }

  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){
          obj[temp]={};
        }
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        try{
          obj[temp] = v;
        }catch(e){
          console.log("error:obj[temp]",`obj[${temp}]`);
        }
        
      }
    };
    loop(this,arr,v);
  };
  
  @action init=()=>this._init();

  
  
  // 点击节点
  @action click = (keys,info)=>{
    this.set("form-data-tid",keys[0]);
    this.set("selectedKeys",keys);
  };

  // 上传图片
  @action uploadChange=(info,tp)=>{
    if(info.file.status === "uploading"){
      this.root.set("loading",true);
    }else if (info.file.status === "done"){
      this.root.set("loading",false);
      this.set("form-data-".concat(tp),info.file.response.data);
    }else if (info.file.status === "error") {
      this.root.alert({status:"error","msg":`${info.file.name} 图片上传失败!.`});
      this.root.set("loading",false);
    }else{
      this.root.set("loading",false);
    }
  };

  _init(){
    this._init_paging();
    this._init_form();
    this.set("loading",{
      sys:false,//获取/设置系统设置
      app:false,//获取/设置APP配置
      postage:false,//获取/设置邮费配置
      tree:false,//获取分类
    });

    this.set("setting",{
      app:{
        name:"",
        version:"",
        versioncode:"",
        download:"",
      },
      sys:{
      },
      // 邮费
      postage:[]
    });
    this.set("postage",{
      company:"",
      first:"",
      continued:"",
      continuePrice:"",
    });
    this.set("companyVisible",false);
    this._get_configura("app");
    this._get_configura("sys");
    this._get_configura("postage");
    this._init_type();
    this._init_tree();
  }

  _init_type(){
    this.set("loading-type",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:'admin-configura-type'},
      success:json=>{
        this.set("loading-type",false);
        if(json.status === "ok"){
          this.set("movieType",json.data);
        }else{
          this.root.alert(json);
        }
      }
    });
  }

  // 点击tab
  @action changeTab=(key)=>this._changeTab(key);
  _changeTab(key){
    switch(key){
      case "home":
        if(!this.treeData.length){
          const tm = setInterval(()=>{
            if(this.setting.app.homeid){
              this._init_tree();
              clearInterval(tm);
            }
          },1000)
          
        }
        break;
      default:
    }
  }
  // 分页数据初始化
  _init_paging(){
    this.set("types",{
      data:[],
      maxPage:1,
      page :1,
      maxNum:1,
      num:20,
    });
  }
  
  // 获取APP配置信息
  @action get_configura=k=>this._get_configura(k);
  _get_configura(k){
    this.set("loading-".concat(k),true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-configura-get",k:k},
      success:json=>{
        this.set("loading-".concat(k),false);
        if(json.status === "ok"){
          const configura=JSON.parse(json.data);
          if(configura){this.set("setting-".concat(k),configura);}
        }else{
          this.root.alert(json);
        }
      }
    });
  }
  
  // 保存配置信息
  @action submit_configura=(k)=>this._submit_configura(k);
  _submit_configura(k){
    console.log(toJS(this.setting));
    this.set("loading-".concat(k),true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:'admin-configura-set',configura:JSON.stringify(toJS(this.setting[k])),k:k},
      success:json=>{
        this.set("loading-".concat(k),false);
        if(json.status === "ok"){
          this.root.alert(json);
        }else{
          this.root.alert(json);
        }
      }
    });
  }

  _init_form(){
    this.set("form",{
      visible:false,
      condition:"insert", // insert|update
      title:"",//窗口标题
      data:{
        id:"",
        movieid:0,
        tid:"",
        tit:"",
        ttit:"",
        tp:0,
        url:"",
        image:"",
        num:0,
      }
    });
  }
  
  @action init_tree=()=>this._init_tree();
  _init_tree(){
    this._find(this.setting.app.homeid,json=>{
      this.set("loading-tree",false);
      if(json.status !== "ok"){this.root.alert(json);return;}
      const treeData = json.data.map(item=>Object.assign({key:item.id,title:item.tit,isLeaf: item.num==="0"},item));
      this.set("treeData",treeData);

      const options=json.data.map(item=>Object.assign({value:item.id,label:item.tit,isLeaf: item.num==="0"},item));
      this.set("options",options);
    });
  }
  // 获取分类数据
  _find(upid,func){
    this.set("loading-tree",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-type-select",upid:upid},
      success:func
    });
  }

  // 加载下拉菜单
  @action onLoadOption = selectedOptions => this._onLoadOption(selectedOptions);
  _onLoadOption(selectedOptions) {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    targetOption.loading = true;
    this.set("loading-tree",true);
    this._find(targetOption.id,json=>{
      targetOption.loading = false;  
      this.set("loading-tree",false);
      if(json.status !== "ok"){
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        return;
      }
      if(json.data.length){
        runInAction(()=>{
          targetOption.children = json.data.map(item=>Object.assign({value:item.id,label:item.tit,isLeaf: item.num==="0",},item));  
        });
        runInAction(()=>this.set("options",[...this.options]));
      }
    });
  }
  
  // 加载分类
  // 加载子节点
  @action onLoadData = treeNode=>this._onLoadData(treeNode);
  _onLoadData(treeNode) {
    return new Promise((resolve) => {
      if (treeNode.props.children) {
        resolve();
        return;
      }
      this.set("loading-tree",true);

      this._find(treeNode.props.id,json=>{
        this.set("loading-tree",false);
        if(json.status !== "ok"){
          this.root.modal.info({
            title:"提示",
            content:json.msg
          });
          return;
        }
        if(json.data.length){
          runInAction(()=>{
            treeNode.props.dataRef.children = json.data.map(
              item=>Object.assign({
                    key:item.id,
                    title:item.tit,
                    isLeaf: item.num==="0"
                  },
                  item
                )
              );
          });
          this.set("treeData",[...this.treeData]);
        }
        resolve();
      });

    });
  }


  
  // 根据名称查询影视
  @action serch_movie=v=>this._serch_movie(v);
  _serch_movie(){
    if(this.loading.movie || !this.searchKey){return;}
    this.set("loading-movie",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-configura-movie-search",v:this.searchKey},
      success:json=>{
        this.set("loading-movie",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this.set("searchData",json.data);
      }
    });
  }



  // 获取专题分页
  @action thematic_paging=(p)=>this._thematic_paging(p);
  _thematic_paging(p){
    if(!this.form.data.tid){return;}
    this.set("loading-thematic",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:'admin-configura-thematic-paging',tid:this.form.data.tid,p:p},
      success:json=>{
        this.set("loading-thematic",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this.set("paging",json.data);
      }
    });
  }
  // 保存专题内容
  @action thematic_submit=()=>{
    this.set("loading-thematicSave",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-configura-thematic-save",data:JSON.stringify(this.form.data),condition:this.form.condition},
      success:json=>{
        this.set("loading-thematicSave",false);
        this.root.alert(json);
        if(json.status !== "ok"){return;}
        // console.log("insert")
        if(this.form.condition === "insert"){
          const tid = this.form.data.tid;
          this._init_form();
          this.set("form-data-tid",tid);
          this._thematic_paging(1);
        }else{
          this._thematic_paging(this.paging.nowPage);
        }
        
        
      }
    });
  };
  // 删除一条
  @action thematic_delete=id=>{
    this.set("loading-thematicDelete",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-configura-thematic-delete",id:id},
      success:json=>{
        this.set("loading-thematicDelete",false);
        this.root.alert(json);
        if(json.status === "ok"){
          this._init_paging();
          this._thematic_paging(1);
        }
      }
    });
  };




}
